package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.Get;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.connection.RedisServer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api("我是菜品管理er")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

//    添加了缓存！！
    @GetMapping("/list")
    public Result<List<DishVO>> kkk(Long categoryId) {
        List<DishVO> res = dishService.dishByCateId(categoryId);
        return Result.success(res);
    }

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result status(@PathVariable Integer status, Long id) {
        dishService.dishChangeStatus(status, id);

//        cleanCache("dish_*");
        return Result.success();
    }


    @PutMapping("")
    @ApiOperation("dish的更新！！")
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result updateDish(@RequestBody DishDTO dishDTO) {

        System.out.println("(😄)你要更新的dishDTO东西传过来了:{}" + dishDTO);
        dishService.dishUpdate(dishDTO);
        System.out.println("(😄)你的dishDTO" + dishDTO + "已经up完成啦！");
//        cleanCache("dish_*");
        return Result.success();

    }

    @GetMapping("{id}")
    public Result<DishVO> findById(@PathVariable Long id) {

        Result<DishVO> result = (Result<DishVO>) redisTemplate.opsForValue().get("dish_"+id);
        if (result != null) {
            return result;
        }

        System.out.println("(😄)你要得到dishVO！通过id," + id);
        result = Result.success(dishService.dishById(id));
        System.out.println("(😄)你要得到了dishVO！" + result.getData().toString());

        redisTemplate.opsForValue().set("dish_"+id, result);

        return result;
    }

    @DeleteMapping("")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        System.out.println("(😄)你要删除dish通过ids," + ids);
        dishService.dishDelete(ids);
        System.out.println("(😄)删除成功啦通过ids," + ids);

//        cleanCache("dish_*");
        return Result.success();
    }

    @PostMapping("")
    @CacheEvict(cacheNames = "dishCache",key = "#dsdsto.categoryId")
    public Result<String> dishAdd(@RequestBody DishDTO dsdsto) {

        System.out.println("(😄)你要加入一个dish，通过DishDTO," + dsdsto);
        dishService.dishAdd(dsdsto);
        System.out.println("(😄)加入一个dish成功，通过DishDTO," + dsdsto);
        String kk = "dish_"+dsdsto.getCategoryId();
//        cleanCache(kk);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<PageResult> dishPage(DishPageQueryDTO dddQto) {

        System.out.println("(😄)你要查询DishPage!通过DishPageQueryDTO," + dddQto);
        PageResult pageResult = dishService.dishPAge(dddQto);
        System.out.println("(😄)成功查询成功啦，得到PageResult," + pageResult);
        return Result.success(pageResult);

    }

//    private void cleanCache(String pattern){
//        Set kk = redisTemplate.keys(pattern);
//        redisTemplate.delete(kk);
//    }

}
